package com.vitabuybuy.tv.ui.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.core.databinding.BooleanObservableField
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.network.ApplyTalentRepository
import com.vitabuybuy.tv.network.MineRepository
import rxhttp.async

class MineViewModel : BaseViewModel() {
    val userInfoAndOrderCount = MutableLiveData<UserInfoAndOrderCount>()
    val userSign = MutableLiveData<UserSingRecordDto>()
    val activityRewards = MutableLiveData<List<ActivityUserRecordDto>>()
    val applyTalentUserInfo = MutableLiveData<TalentUserInfoDto>()
    val isFirstLoading = BooleanObservableField()
    fun getUserInfoAndOrderCount() {
        rxHttpRequest {
            onRequest = {
                val value1 = MineRepository.getInfoByToken().async(this)
                val value2 = MineRepository.getOrderCount().async(this)
                userInfoAndOrderCount.value = UserInfoAndOrderCount(value2.await(), value1.await())
                statusView.set(LoadingType.SHOW_CONTENT)
                isFirstLoading.set(false)
            }
            onError = {
                XToast.normal(it.msg)
                isFirstLoading.set(true)
            }
            isLoadingType = true
            isRefresh = isFirstLoading.get()
        }
    }

    fun isSign() {
        rxHttpRequest {
            onRequest = {
                userSign.value = MineRepository.userIsSign().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun userSign() {
        rxHttpRequest {
            onRequest = {
                MineRepository.userSign().await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getActivityRewards() {
        rxHttpRequest {
            onRequest = {
                activityRewards.value = MineRepository.getActivityRewards().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getTalentUserInfo() {
        rxHttpRequest {
            onRequest = {
                applyTalentUserInfo.value = ApplyTalentRepository.applyTalentUserInfo().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
